﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace refocusing
{
    public class XmlManager
    {
        public static XmlDocument CreateXml(String name)
        {
            XmlDocument xml = new XmlDocument();
            XmlNode child = xml.CreateElement(name);
            xml.AppendChild(child);

            return xml;
        }

        public static String GetXmlAsString(XmlDocument xml)
        {
            StringWriter stringWriter = new StringWriter();
            xml.WriteTo(new XmlTextWriter(stringWriter));
            return stringWriter.ToString();
        }

        public static void AddAttributeToXml(XmlDocument xml, XmlNode nodeForAttr, String attrName, String attrValue)
        {
            if (nodeForAttr != null)
            {
                XmlAttribute attr = xml.CreateAttribute(attrName);
                attr.Value = attrValue;
                nodeForAttr.Attributes.Append(attr);
            }
        }

        public static XmlNode AddNodeToXml(XmlDocument xml, XmlNode parentNode, String nodeName)
        {
            XmlNode child = null;

            if (parentNode != null)
            {
                child = xml.CreateElement(nodeName);
                parentNode.AppendChild(child);
            }

            return child;
        }

        public static void AddTextToNode(XmlDocument xml, XmlNode parentNode, String nodeText)
        {
            if (parentNode != null)
            {
                XmlText text = xml.CreateTextNode(nodeText);
                parentNode.AppendChild(text);
            }
        }
    }
}
